<?php

require_once $_SERVER['srcPath'].'/MessageFromApp.php';
require_once $_SERVER['srcPath'].'/ApplicationTCPConnection.php';
require_once $_SERVER['srcPath'].'/Db.php';
use GrouppaGroup\MessageFromApp;
use GrouppaGroup\Db;
use GrouppaGroup\ApplicationTCPConnection;

/**
 * Some common functions for other tests
 * @author Sylwester Kogowski
 *
 */
class TestUtils
{
	
	/**
	 * creates a new MessageFromApp based on parameters
	 * @param string $from
	 * @param number $id
	 * @param string $serializedArguments
	 * @param string $task
	 * @param string $toController
	 * @param string $toGroup
	 * @param string $messageType
	 * @return \GrouppaGroup\MessageFromApp
	 */
	static public function createMessageFromApp($from='',$id=0,$serializedArguments='',$task='',$toController='',$toGroup='',$messageType='')
	{
		$message = new MessageFromApp();
		
		$message->from = $from;
		$message->id = $id;
		$message->serializedArguments = $serializedArguments;
		$message->task = $task;
		$message->toController = $toController;
		$message->toGroup = $toGroup;
		$message->messageType = $messageType;
		
		return $message;
	}
	
	static public function getLastMessageParams()
	{
		$message = ApplicationTCPConnection::$responses[count(ApplicationTCPConnection::$responses)-1];
		return $message->serializedArguments;
	}
	
	static public function getAutoIncrementValue($table)
	{
		$result = Db::getInstance()->sendQuery("SHOW TABLE STATUS LIKE '$table';");
		$row = $result->fetch_assoc();
		return $row['Auto_increment'];
	}
	
	/**
	 * gets last response of server
	 * @throws \Exception
	 * @return unknown serialized arguments of last response
	 */
	static public function getLastResponseParameters($errorIfNone=true,$messageType=null)
	{

		$nResponses = count(ApplicationTCPConnection::$responses);
		if($nResponses <= 0)
		{
			if($errorIfNone)
				throw new \Exception('No response detected');
			else 
				return null;
		}
		$lastMessage = array_pop(ApplicationTCPConnection::$responses);
		if($messageType != null)
		{
			if($lastMessage->messageType != $messageType)
				return null;
		}
		$args = $lastMessage->serializedArguments;
		if(!$args)
		{
			throw new \Exception('No arguments detected');
		}
		return $args;
	}
	
	/**
	 * checks if in last response , the arguments provided in serializedArguments match
	 * those provided to this method, it doesn't check parameters not provided
	 * @param array $arguments associative array with expected arguments of response
	 */
	static public function assertLastResponseArguments(array $arguments)
	{
		$nResponses = count(ApplicationTCPConnection::$responses);
		if($nResponses <= 0)
			throw new \Exception('No response detected');
		$lastMessage = array_pop(ApplicationTCPConnection::$responses);
		if($lastMessage->messageType == 'error')
		{
			throw new \Exception('Expected return message, instead got an error message: '.$lastMessage->serializedArguments);
		}
		$args = $lastMessage->serializedArguments;
		if(!$args)
		{
			throw new \Exception('No arguments detected');
		}
		$argsObject = json_decode($args);
		if(!$argsObject)
		{
			throw new \Exception('Expected object, got something else: '.$args);
		}
		foreach ($arguments as $key => $value)
		{
			if(!array_key_exists($key, $argsObject))
			{
				throw new \Exception("Expected argument $key in response, but not found");
			}
			if($value != $argsObject->$key)
			{
				throw new \Exception("Expected that '$key'='$value' in response, but instead found '{$argsObject->$key}'");
			}
		}
	}
}